package com.hibernate.services;

import com.hibernate.model.Customer;
import com.hibernate.model.Dictionary;
import com.hibernate.model.Order;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import java.util.HashSet;

public class BusinessService {
    public static SessionFactory sessionFactory;
    static {
        try {
            //加载hibernate.cfg.xml文件中配置的信息
            Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    public Customer loadCustomer(long customer_id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = (Customer) session.get(Customer.class, new Long(customer_id));
            tx.commit();
            return customer;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void saveCustomer(Customer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(customer);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void loadAndUpdateCustomer(long customerId) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Customer customer = (Customer) session.get(Customer.class, new Long(customerId));
            customer.setDescription("A lovely customer!");
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void updateCustomer(Customer customer) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.update(customer);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void saveDictionary(Dictionary dictionary) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(dictionary);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            System.out.println();
            session.close();
        }
    }

    public void updateDictionary(Dictionary dictionary) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.update(dictionary);
            tx.commit();
            System.out.println();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }
    public Dictionary loadDictionary(long dictionary_id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Dictionary dictionary = (Dictionary) session.get(Dictionary.class, new Long(dictionary_id));
            tx.commit();
            return dictionary;
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    public void printCustomer(Customer customer) {
        System.out.println("name:" + customer.getName());
        System.out.println("sex:" + customer.getSex());
        System.out.println("description:" + customer.getDescription());
        System.out.println("avgPrice:" + customer.getAvgPrice());
        System.out.println("totalPrice:" + customer.getTotalPrice());
    }

    public void printDictionary(Dictionary dictionary) {
        System.out.println("type:" + dictionary.getType());
        System.out.println("key:" + dictionary.getKey());
        System.out.println("value:" + dictionary.getValue());
    }
    public void test() {
        Customer customer = new Customer("Laosan", "Zhang", 'M', new HashSet(), "A good citizen!");
        Order order1 = new Order("Order001", new Double(100), customer);
        Order order2 = new Order("Order002", new Double(200), customer);
        customer.getOrders().add(order1);
        customer.getOrders().add(order2);

        saveCustomer(customer);

        customer = new Customer("Laowu", "Wang", 'M', new HashSet(), "NOT-null");
        saveCustomer(customer);

        customer = loadCustomer(1);
        printCustomer(customer);

        customer.setDescription("An honest customer!");
        updateCustomer(customer);

        loadAndUpdateCustomer(1);

        Dictionary dictionary = new Dictionary("SEX", "M", "MALE");
        saveDictionary(dictionary);

        dictionary = loadDictionary(1);
        dictionary.setValue("MAN");
        updateDictionary(dictionary);

        dictionary = loadDictionary(1);
        printDictionary(dictionary);

    }

    public static void main(String args[]) {
        new BusinessService().test();
        sessionFactory.close();
    }
}
